package com.firefly.service.command.ums;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.firefly.dao.dao.ums.po.MenuPo;
import com.firefly.dao.dao.ums.po.RoleAdminPo;
import com.firefly.dao.dao.ums.po.RolePo;
import com.firefly.dao.dao.ums.po.RolePermissionPo;
import com.firefly.dao.dao.ums.service.MenuService;
import com.firefly.dao.dao.ums.service.RoleAdminService;
import com.firefly.util.handle.FireflyException;
import com.firefly.dao.assembler.ums.RoleAssembler;
import com.firefly.dao.assembler.ums.RolePermissionAssembler;
import com.firefly.dao.dto.CommonDto;
import com.firefly.dao.dto.ums.RoleAdminDto;
import com.firefly.dao.dto.ums.RoleDto;
import com.firefly.dao.dto.ums.RoleMenuDto;
import com.firefly.service.repository.ums.RoleAdminRepository;
import com.firefly.service.repository.ums.RolePermissionRepository;
import com.firefly.service.repository.ums.RoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * @description: 角色管理
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2022/12/5
 **/
@Service
public class RoleCommand {
    private final RoleAssembler roleAssembler;
    private final RolePermissionAssembler rolePermissionAssembler;
    private final RoleAdminService roleAdminService;
    private final MenuService menuService;
    private final RoleRepository roleRepository;
    private final RolePermissionRepository rolePermissionRepository;
    private final RoleAdminRepository roleAdminRepository;
    @Autowired
    public RoleCommand(RoleRepository roleRepository, RolePermissionRepository rolePermissionRepository, RoleAdminRepository roleAdminRepository, RolePermissionAssembler rolePermissionAssembler, RoleAssembler roleAssembler, RoleAdminService roleAdminService, MenuService menuService){
        this.roleAssembler = roleAssembler;
        this.rolePermissionAssembler = rolePermissionAssembler;
        this.roleRepository = roleRepository;
        this.rolePermissionRepository = rolePermissionRepository;
        this.roleAdminRepository = roleAdminRepository;
        this.roleAdminService = roleAdminService;
        this.menuService = menuService;
    }
    public Boolean create(RoleDto request){
        RolePo entity = roleAssembler.getPo(request);
        return roleRepository.create(entity);
    }
    //	更新单条名称
    public Boolean update(RoleDto request) {
        RolePo entity = roleAssembler.getPo(request);
        return roleRepository.update(entity);
    }

    //	更新单条状态
    public Boolean updateStatus(RoleDto request) {
        RolePo entity = roleAssembler.getPo(request);
        return roleRepository.updateStatus(entity);
    }
    //	删除单条,同时删除关联的权限信息
    @Transactional
    public Boolean remove(CommonDto request) throws Exception {
        int roleId = request.detailId;
        // 判断是不是创始人
        if (roleId == 1) {
            throw new FireflyException(403, "超级管理员角色不能删除");
        }
        // 先检查部门下有没有用户
        QueryWrapper<RoleAdminPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        if (roleAdminService.count(queryWrapper) > 0) {
            throw new FireflyException(403,"角色下有用户");
        }
        rolePermissionRepository.removePrivByRoleId(roleId);
        return roleRepository.removeById(roleId);
    }

    @Transactional
    public Boolean removeMultiple(CommonDto request) throws Exception {
        List<Integer> roleIds = request.detailIds;
        // 判断是不是创始人
        if (roleIds.contains(1)) {
            throw new FireflyException(403, "超级管理员角色不能删除");
        }
        // 先检查部门下有没有用户
        QueryWrapper<RoleAdminPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("role_id", roleIds);
        if (roleAdminService.count(queryWrapper) > 0) {
            throw new FireflyException(403,"角色下有用户");
        }
        // 同时删除关联的权限信息
        rolePermissionRepository.removePrivByRoleId(roleIds);
        return roleRepository.removeById(roleIds);
    }



    // 修改权限
    @Transactional
    public Boolean postPriv(RoleMenuDto request) {
        // 删除旧的
        rolePermissionRepository.removePrivByRoleId(request.getId());
        // 将查出来的数据组成数组插入到role_privs表里
        List<Integer> menuIds = request.getMenuIds();
        if (menuIds.size() > 0) {
            QueryWrapper<MenuPo> listWrapper = new QueryWrapper<>();
            listWrapper.in("id", menuIds);
            List<MenuPo> all = menuService.list(listWrapper);
            Integer roleId = request.getId();
            List<RolePermissionPo> inserts = rolePermissionAssembler.getListPo(roleId,all);
            rolePermissionRepository.insertRolePermission(inserts);
        }
        return true;
    }
    // 移除用户
    public Boolean postRemoveAdmin(RoleAdminDto request) {
        return roleAdminRepository.removeAdminByRoleId(request.getId(),request.getAdminId());
    }
}
